package com.lh.service.Impl;

import com.lh.entity.Category;
import com.lh.entity.Result;
import com.lh.mapper.CategoryMapper;
import com.lh.service.CategoryService;
import com.lh.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class CategoryServiceImpt implements CategoryService {

    @Autowired
    CategoryMapper categoryMapper;
    @Override
    public Result addCategoryType(Category category) {
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
//        封装数据
        category.setCreateUser(userId);
       int i= categoryMapper.addCategoryType(category);
       if (i>0){
           return Result.success("文章类别插入成功");
       }else{
           return Result.error("文章类别插入失败");
       }


    }

    @Override
    public List<Category> categoryList() {
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
     return    categoryMapper.categoryList(userId);
    }

    @Override
    public Category getCategory(int id) {
       Category category= categoryMapper.getCategory(id);
        return category;
    }

    @Override
    public Result updateCategory(Category category) {
       int i= categoryMapper.updateCategory(category);
        if (i>0){
            return Result.success("文章类别更新成功");
        }else{
            return Result.error("文章类别更新失败");
        }

    }

    @Override
    public Result deleteCategory(int id) {
      int i= categoryMapper.deleteCategory(id);
        if (i>0){
            return Result.success("文章类别删除成功");
        }else{
            return Result.error("文章类别删除失败");
        }
    }
}
